﻿using System.Collections.Generic;
using UnityEngine;
using Yoozoo.Gameplay.RTS;

public class RpgCameraViewCleaner : MonoBehaviour
{
    private Camera camera;

    private List<GameObject> hideList = new List<GameObject>();
    
    public void HideSceneObjects()
    {
        if (!camera)
        {
            camera = GetComponent<Camera>();
        }

        if (camera)
        {
            var leftBottomPoint = BattleUtils.GetGroundPositionByScreenPosition(camera,new Vector2(0, 0));
            var rightBottomPoint = BattleUtils.GetGroundPositionByScreenPosition(camera,new Vector2(Screen.width, 0));
            var centerPoint = (leftBottomPoint + rightBottomPoint) / 2;
            
            var leftTopPoint = camera.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, camera.nearClipPlane));

            var width = Vector3.Distance(leftBottomPoint, rightBottomPoint);
            var height = leftTopPoint.y - leftBottomPoint.y;
            var thickness = 10f;

            var forward = camera.transform.forward;
            forward.y = 0;

            var center = centerPoint + new Vector3(0, height / 2 + 0.5f, 0);

            hideList.Clear();
            
            var rayCastHits = Physics.BoxCastAll(center, new Vector3(width/2, height/2,thickness/2),forward,Quaternion.identity,0.01f);
            for (int i = 0; i < rayCastHits.Length; i++)
            {
                var cast = rayCastHits[i];
                var gameObject = cast.transform.gameObject;
                if (!gameObject.name.Contains("Deck") && !gameObject.name.Contains("_ground_") && !gameObject.name.Contains("building"))
                {
                    gameObject.SetActive(false);
                    hideList.Add(gameObject);
                }
            }
        }
    }

    public void ShowSceneObjects()
    {
        for (int i = 0; i < hideList.Count; i++)
        {
            if (hideList[i] != null)
            {
                hideList[i].SetActive(true);
            }
        }
        hideList.Clear();
    }
    
}
